package com.elitel.frame.web.cfgservergroup.service.impl;

import com.elitel.common.utils.SysUserUtils;
import com.elitel.frame.web.cfgservergroup.dao.CfgServerGroupDao;
import com.elitel.frame.web.cfgservergroup.entity.CfgServerGroup;
import com.elitel.frame.web.cfgservergroup.service.CfgServerGroupService;
import com.elitel.frame.web.common.idworker.IdWorker;
import com.elitel.frame.web.utils.StringUtils;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.util.Date;
import java.util.List;

/**
 * 数据分组表(CfgServerGroup)表服务实现类
 *
 * @author makejava
 * @since 2025-05-20 09:31:35
 */
@Service("cfgServerGroupService")
public class CfgServerGroupServiceImpl implements CfgServerGroupService {
    @Resource
    private CfgServerGroupDao cfgServerGroupDao;

    /**
     * 通过ID查询单条数据
     *
     * @param id 主键
     * @return 实例对象
     */
    @Override
    public CfgServerGroup queryById(String id) {
        return this.cfgServerGroupDao.queryById(id);
    }

    @Override
    public List<CfgServerGroup> list(CfgServerGroup cfgServerGroup) {
        return cfgServerGroupDao.queryList(cfgServerGroup);
    }

    /**
     * 新增数据
     *
     * @param cfgServerGroup 实例对象
     * @return 实例对象
     */
    @Override
    public CfgServerGroup insert(CfgServerGroup cfgServerGroup) {
        cfgServerGroup.setId(IdWorker.createStrId());
        cfgServerGroup.setCreatedTime(new Date());
        cfgServerGroup.setUpdatedTime(new Date());
        cfgServerGroup.setCreatedUser(SysUserUtils.getLoginAccount());
        cfgServerGroup.setUpdatedUser(SysUserUtils.getLoginAccount());
        cfgServerGroup.setDelFlag(0);
        this.cfgServerGroupDao.insert(cfgServerGroup);
        return cfgServerGroup;
    }

    /**
     * 修改数据
     *
     * @param cfgServerGroup 实例对象
     * @return 实例对象
     */
    @Override
    public CfgServerGroup update(CfgServerGroup cfgServerGroup) {
        cfgServerGroup.setUpdatedTime(new Date());
        cfgServerGroup.setUpdatedUser(SysUserUtils.getLoginAccount());
        this.cfgServerGroupDao.update(cfgServerGroup);
        return this.queryById(cfgServerGroup.getId());
    }

    /**
     * 通过主键删除数据
     *
     * @param id 主键
     * @return 是否成功
     */
    @Override
    public boolean deleteById(String id) {
        return this.cfgServerGroupDao.deleteById(id) > 0;
    }

    @Override
    public CfgServerGroup save(CfgServerGroup cfgServerGroup) {
        String byId = cfgServerGroup.getId();
        if (StringUtils.isEmpty(byId)) {
            return this.insert(cfgServerGroup);
        } else {
            return this.update(cfgServerGroup);
        }
    }
}
